﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.intellitack.layar.library
{
    public abstract class AbstractLayarObject:ILayerObject
    {
        protected virtual string SerializeProperty(string key, object value) 
        {
            if (value is string)
            {
                //Simply creates a formatted key/value pair with quotes
                return string.Format("\"{0}\":\"{1}\"", key, value);
            }
            else if (value is Boolean?) 
            {
                //I'm reading the specification literally, so I'm returning lower case true and false.  .Net's
                //ToString() return 'T'rue and 'F'alse.
                if ((Boolean)value)
                {
                    return string.Format("\"{0}\":{1}", key, "true");     
                }
                else 
                {
                    return string.Format("\"{0}\":{1}", key, "false");
                }

            }
            else
            {
                if (value != null)
                {
                    //Simply creates a formatted key/value pair without quotes
                    return string.Format("\"{0}\":{1}", key, value);
                }
                else
                {
                    //Simply creates a formatted key/value pair with null specifically shown
                    return string.Format("\"{0}\":null", key);
                }
            }
        }

        protected virtual string LayarEncodeURL(string value) 
        { 
            //Addess the escape character for forward slashes.
            return value.Replace("/","\u005C\u002F");
        }

        protected virtual string SerializeEnumeration(string key, System.Collections.IList collection) 
        {
            //Create a new String Builder to hold the value
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("\"{0}\":", key));
            //Open the Object Array
            sb.Append("[");
            //Iterate through the collection of Layar Objects
            for (int i = 0; i < collection.Count; i++)
            {
                //Grab the AbstractLayarObject from the collection
                AbstractLayarObject layarObject = collection[i] as AbstractLayarObject;
                if (layarObject != null)
                {
                    //Append the Serialized Object
                    sb.Append(layarObject.Serialize());
                    //Don't append a "," if we are on the last one.
                    if (i < (collection.Count - 1))
                    {
                        sb.Append(",");
                    }
                }
            }
            //Close the Object Array
            sb.Append("]");
            return sb.ToString();
            //throw new NotImplementedException();
        }

        public abstract string Serialize();
    }
}
